﻿using lib;
using System;
using System.IO;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;

namespace Server
{
    class Docclient
    {
        private TcpClient client;
        public int selectedID { get; private set; }
        private Server server;
        public SecureConnectionServer secureConnection { get; set; }
        private SavedFiles savedFiles;
        private BinaryFormatter formatter = new BinaryFormatter();

        public Docclient(Server s, SecureConnectionServer sc)
        {
            server = s;
            secureConnection = sc;
            savedFiles = new SavedFiles();
        }

        // Author 
        // Handle client (save or load file depending on the input data).
        public void Handle(Object obj)
        {
            DocClientHandler docclienthandler = obj as DocClientHandler;
            client = docclienthandler.docClient;
            selectedID = 0;
            String[] split = new String[3]; 
            if (secureConnection == null)
            {
                secureConnection = new SecureConnectionServer(client, Server.getCertificate());
            }

            // Read data and save or load file here.
            while (true)
            {
                try
                {
                    split = secureConnection.ReadObjectAsString().Split(':');
                }
                catch
                {
                    secureConnection.ClosesslStream();
                    ClientHandler.tcpDoc = null;
                    Console.WriteLine("ERROR: Connection to doc lost");
                    break;
                }

                if (split[0] == "chat")
                {                    
                    foreach (Client s in server.getClients())
                    {
                        if (split[2] == "all")
                        {
                            s.secureConnection.SendObject("chat:" + split[1]);
                            Console.WriteLine("chat from doctor to " + split[1]);
                        }

                        else if (s.clientHandle.patientID == Convert.ToInt32(split[2]))
                        {
                            s.secureConnection.SendObject("chat:" + split[1]);
                            Console.WriteLine("chat from doctor to " + split[1]);
                            break;
                        }
                    }
                }
                else if (split[0] == "BikeCommand")
                {
                    foreach (Client c in server.getClients())
                    {
                        if (c.getID() == Convert.ToInt32(split[1]))
                        {
                            c.secureConnection.SendObject("BikeCommand:" + split[2] + ":" + split[3]);
                        }
                    }
                }

                else if (split[0] == "realTimeData")
                {
                    selectedID = Convert.ToInt32(split[1]);
                    Console.WriteLine("realtime data van: " + split[1]);
                }

                else if (split[0] == "dataPacket")
                {
                    foreach (Client c in server.getClients())
                    {
                        if (c.getID() == Convert.ToInt32(split[1]))
                        {
                            //formatter.Serialize(client.GetStream(), "dataPacket:" + c.TestResultFile.getString());
                            secureConnection.SendObject("dataPacket:" + c.TestResultFile.getString());
                            Console.WriteLine("dataPacket: " + split[1]);
                        }
                    }
                }
                else if (split[0] == "getSavedFilesList")
                {
                    savedFiles.RefreshFiles();
                    server.getDocClient().secureConnection.SendObject("savedFilesList:" + savedFiles.getAllFilesInString());
                }
                else if (split[0] == "getSavedFile")
                {
                    FileStream fs = savedFiles.getFileStream("SavedFiles/" + split[1]);
                    TestResult result = formatter.Deserialize(fs) as TestResult;
                    Console.WriteLine(result.getString());

                    //Send 'result' to Doc. How?
                    server.getDocClient().secureConnection.SendObject(result);
                }
            }
        }
    }
}
